/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.questionnaire;

import org.ipti.br.tag.bean.AppBaseBean;
import org.ipti.br.tag.bean.SessionBean;
import org.ipti.br.tag.bo.CampaignBO;
import org.ipti.br.tag.bo.HbBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import org.ipti.br.tag.entity.enums.CampaignType;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author FelipeMSX
 */
@Named(value="letterToParentsB") @ViewScoped
public class LetterToParentsBean extends AppBaseBean implements Serializable {
    
   @Inject private SessionBean sessionBean;
   @Inject private HbBO hbBO;
   @Inject private UnityBO unityBO;
   @Inject private CampaignBO campaignBO;

   private Unity unity;
   private Campaign campaign;
   
   private int year = getActualYear();
   
   @PostConstruct
   public void init(){
       // Itens necessários para iniciar a página.
       this.campaign = null;
       this.unity = null;
   }  
   
      
   public List<Unity> getUnityList(){
       if (this.campaign != null){ 
           return unityBO.getClassLessonList(sessionBean.getLoggedActor().getUnity(),year);
       }else{
           return null;
       }
   }
   
   public List<Campaign> getCampaignList(){        
      return campaignBO.findAllCampaign(CampaignType.ANEMIA_FERROPRIVA);
   }
   
   public void create(){
       if (campaign != null && unity != null) {
           List<Human> humanListAnemic = hbBO.letterToParentsCreateList(campaign, unity);
           createPdf(humanListAnemic);
       } else {
           addErrorMessage(":createConsentForm:message", "A campanha e/ou a unidade não foram selecionados",
                   "Recarregar a página e tente novamente");
       }
   }
   
   public void createPdf(List<Human> humanListAnemic){
        if( humanListAnemic.isEmpty() ){
            addErrorMessage(":createConsentForm:message", "Não encontramos nenhum anêmico", "Estranho em? o.O");
        }else{
            boolean sucess =  hbBO.generateLetterToParendsPdf(humanListAnemic, campaign, unity, sessionBean.getLoggedActor().getUnity());
            if (sucess) {
                addInfoMessage(":createConsentForm:message", "Sucesso!", "Os relatórios gerados estão lindos, né?");
            } else {
                addErrorMessage(":createConsentForm:message", "Ocorreu algum provlema ao gerar os relatórios", ":(");
            }
        }
   }
   
   public boolean activeUnity(){
       return ( this.campaign == null );
   }
   
   public boolean activeBottom(){
       return ( ( this.campaign == null ) || ( this.unity == null ) );
   }
    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public Campaign getCampaign() {
        return campaign;
    }

    public void setCampaign(Campaign campaign) {
        this.campaign = campaign;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}
